<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
	String studentno = null;
	if (null != request.getAttribute("studentno")) {
		studentno = (String) request.getAttribute("studentno");
	}
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">

		<title>模糊查询</title>

		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<link rel="stylesheet"
			href="<%=request.getContextPath()%>/css/common.css" type="text/css" />
		<link rel="stylesheet"
			href="<%=request.getContextPath()%>/css/style.css" type="text/css" />

		<script type='text/javascript'
			src='<%=request.getContextPath()%>/js/sorttable.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/interface/dWRCollegeMajorAction.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/interface/studentAction.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/engine.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/util.js'></script>
		<script type="text/javascript"
			src="<%=request.getContextPath()%>/js/N13FormValidation-2.js"></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/js/checkform.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/js/commonValidate.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/js/jquery-latest.pack.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/js/noConflictOfJquery.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/js/forCSS.js'></script>

		
		
		<script type='text/javascript'>
	if(null != <%=studentno%>){
		alert('修改成功!');
	}
	
	//全局变量
	var totalcount = -1;
	var page;   //dwr分页的page对象，注意在页面中是一个Json对象
	var seleFlag ; //标记选择是学号还是姓名 ,0为学号,1为姓名
	var stu;
	var s_sql;
	

	//查询满足条件的学生的总数回调函数
	var searchStudentTotalRowsCallback = function searchStudentTotalRowsCallbackFunc(count){
		if(count == 0){
			alert("没有此学生的信息");
			return;
		}
		totalcount = count;
		//alert("@@"+totalcount)
		if(seleFlag == 0){
			 s_sql = "select * from student where studentind = 1 and studentno like '%" + stu + "%'"; 
		}else{
			 s_sql = "select * from student where studentind = 1 and studentname like '%" + stu + "%'";
		}
		studentAction.getStudentBySQLSearchConditionsPerPage(1,totalcount,s_sql,getStudentInfoCallback);
	}
	
	//先查询要分页的总行数
    var getStudentInfoCallback = function getStudentInfoCallbackFunc(stud){
    	//如果没有重名的学生就直接显示
		//如果有多个学生就先显示列表
		//sl是学生列表
		//alert(stud)
		if(stud == null){
			alert("没有该学生的信息!");
			return;
		}

		
    	if(stud.length == 1){
    		//隐藏前几次的查询结果列表
			$('slTable').style.display = "none";
			$('pageTable').style.display = "none";
			//把学号赋给basicstudentinfo.jsp搜索框
    		$('student_info').value = stud[0][1];
    		$('student_basic_info').style.display = '';
		    $('student_basic_info_search').style.display = "none";
    		stu_BasicInfo();
    	}else{
    		$("slTable").style.display = '';
	        if($('slTable').style.visibility == 'visible'){
	            $('slTable').style.visibility = 'hidden';
	        }
	        if($('pageTable').style.visibility == 'hidden'){
	            $('pageTable').style.visibility = 'visible';
	        }
	        DWRUtil.removeAllRows('ttb', { filter:function(tr) {
	              return (tr.id != "pattern");
	                }});
	        var al,id;
			for (var i =0; i < stud.length ; i++) {
			     al = stud[i];
			     id = al[0];
			     dwr.util.cloneNode("pattern", { idSuffix:id });
			     
			      dwr.util.setValue("sid" + id, al[0]);
			     dwr.util.setValue("sno" + id, al[1]);
			    	dwr.util.setValue("sname" + id, al[2]);
			      dwr.util.setValue("sgender" + id, al[3]);
			      
			      if('object' == typeof(al[4])){
			      		 dwr.util.setValue("sclass" + id, "");
			      }else{
			     		 dwr.util.setValue("sclass" + id, al[4]);
			      }
			      
			      if('object' == typeof(al[5])){
			      		 dwr.util.setValue("scollege" + id, "");
			      }else{
			     		 dwr.util.setValue("scollege" + id, al[5]);
			      }
			      
			      if('object' == typeof(al[6])){
			      		 dwr.util.setValue("smajor" + id, "");
			      }else{
			     		 dwr.util.setValue("smajor" + id, al[6]);
			      }
			      
			      if('object' == typeof(al[7])){
			      		 dwr.util.setValue("sdorm" + id, "");
			      		 
			      }else{
			     		 dwr.util.setValue("sdorm" + id, al[7]);
			      }
			 
			     $("pattern" + id).style.display= "";
			}
			setPageInfo(); //这里写页面信息
	        tbCSS();
	             //这里是奇偶行不一致的css写法
	         /*
			var Ptr= document.getElementById('slTable').getElementsByTagName('tr'); 
			for (i=1;i<Ptr.length+1;i++) {
			  Ptr[i-1].className = (i%2>0)?"row_odd":"row_even";
			}
			*/
	    }       
	  
	     
    }
    
	//单击‘详细信息’查看学生详细信息
	//num是动态表格slTable的行号

	function getStudentInfoDetail(obj){
		var num = obj.id.substr(7);
		$('student_info').value = $('sno'+num).innerHTML;
		$('student_basic_info').style.display = '';
		$('student_basic_info_search').style.display = "none";
		stu_BasicInfo();
	}
	function getStudentInfo(){
		$('student_basic_info').style.display = 'none';		
		stu = strTrim_cv($('studentForSearch').value);
		
		if(stu == ""){
				alert("输入不能为空！");
				return ;
		}
		if(!cnChar_cv(stu)){
				alert("您只能输入数字,字母和汉字,请重新输入");
				return;
		}
		
    	//判断搜索条件是选中的了学号还是选中姓名
    	//todo
		if($('searchType').options[$('searchType').selectedIndex].value == "stuno"){
			seleFlag = 0;
			//alert(stu)
			studentAction.searchStudentByStudentDimNoTotalRows(stu ,searchStudentTotalRowsCallback);
		}else{
			seleFlag = 1;
			studentAction.searchStudentByStudentNameTotalRows(stu ,searchStudentTotalRowsCallback);
			
		}
	}

    function setPageInfo(){
        $('pageTd').innerHTML = '';
        $('pageSelect').style.display = 'none';
        studentAction.getPage({  //调用action层getPage()方法
            callback:function(data){
                page = data;
                //显示 当前页/总页数
                if(page.totalPage != 0){
                    var pageSpan = document.createElement('span');
                    pageSpan.innerHTML = page.currentPage + '/' + page.totalPage;
                    $('pageTd').appendChild(pageSpan);
                }
                //显示 首页 前一页
                if(page.currentPage > 1){
                    var aFirst = document.createElement('a');
                    aFirst.setAttribute('href','javascript:getFirstData()');
                    aFirst.innerHTML = '首页';
                    $('pageTd').appendChild(aFirst);
                    var aPrevious = document.createElement('a');
                    aPrevious.setAttribute('href','javascript:getPreviousData()');
                    aPrevious.innerHTML = '前一页';
                    $('pageTd').appendChild(aPrevious);
                }
                //显示 下一页 末页
                if(page.currentPage < page.totalPage){
                    var aNext = document.createElement('a');
                    aNext.setAttribute('href','javascript:getNextData()');
                    aNext.innerHTML = '下一页';
                    $('pageTd').appendChild(aNext);
                    var aLast = document.createElement('a');
                    aLast.setAttribute('href','javascript:getLastData()');
                    aLast.innerHTML = '末页';
                    $('pageTd').appendChild(aLast);
                }
                //显示下拉框
             
                if(page.totalPage != 1){
                    $('pageSelect').innerHTML = '';
                    $('pageSelect').style.display = '';
                    for(var i=1; i<=page.totalPage; i++){
                        var option = document.createElement('option');
                        option.setAttribute('value', i);
                        if(i == page.currentPage){
                            option.setAttribute('selected', 'selected');
                        }
                        option.innerHTML = i;
                        $('pageSelect').appendChild(option);
                    }
                }
            }
        });
        $('loading').style.display = 'none';
    }

    function getFirstData(){
        page.currentPage = 1;
        getDataPerPage();
    }
    
    function getPreviousData(){
        page.currentPage--;
        getDataPerPage();
    }
    
    function getNextData(){
        page.currentPage++;
        getDataPerPage();
    }
    
    function getLastData(){
        page.currentPage = page.totalPage;
        getDataPerPage();
    }
    
    function gotoPage(select){
        page.currentPage = select.options[select.selectedIndex].value;
        getDataPerPage();
    }
    
    
    function getDataPerPage(){
        $('loading').style.display = '';
        //调用方法
        if(seleFlag == 0){
			studentAction.getStudentBySQLSearchConditionsPerPage(page.currentPage,totalcount, s_sql,getStudentInfoCallback);
		}else{
			studentAction.getStudentBySQLSearchConditionsPerPage(page.currentPage,totalcount, s_sql,getStudentInfoCallback);
		}
    }    
	//enter键按下提交搜索
  
  function   subEvent(event)   {  
	  event = event ? event : (window.event ? window.event : null);    
	  if(event.keyCode==13) {
	  	 getStudentInfo();
	  }
   
  } 

  window.onload = function(){
  		//alert(1);
  		$('student_basic_info').style.display = 'none';	
		var nav_a=$j('#page_nav a');
		var nav_span=$j("#page_nav a span");
		for(var u = 0 ;u<nav_a.length;u++){
				nav_a.get(u).style.backgroundPosition="left top";
				nav_span.get(u).style.backgroundPosition="right top";							
		}
		$j('#page_nav a').get(0).style.backgroundPosition="0% -42px";
		$j("#page_nav a span").get(0).style.backgroundPosition="100% -42px";
  }
  //解除basicsudentinfo.jsp里的enter键事件,firefox下还有问题
 $j(function(){
 		$j('#slTable').get(0).style.display = 'none';
 		$j('#pattern').get(0).style.display = 'none';
 		$j(document).unbind("keydown",subEv);
 		$j(document).bind("keydown",subEvent);
 })

	</script>
	</head>

	<body  style="margin:0px;padding:0px">
		<div id="page_container">
			<jsp:include page="studentsearchnav.jsp"></jsp:include>
			<h2>
				模糊查询
			</h2>
			<div id="basicSearch">
				<form id="stuForm" action="student/updatestudent.action"
					method="post">
					<select class="common_select" id="searchType" 　name="searchType">
						<option value="stuno">
							学号
						</option>
						<option value="stuname">
							姓名
						</option>
					</select>
					<input type="text" name="studentForSearch" id="studentForSearch"
						value="" />

					<input class="common_button" type="button" value="搜索"
						onclick="getStudentInfo()" />
			</div>

			<span> <!-- 显示通过姓名查询出学生列表，因可能存在重名 -->
				<div>
					<table id="slTable" class='table_common table_list_style sortable' rowclass='row_odd,row_even'>
						<thead>
							<tr>
								<th width="0px">

								</th>
								<th width="80px">
									学号
								</th>
								<th width="60px">
									姓名
								</th>
								<th width="40px">
									性别
								</th>
								<th width="130px">
									学院
								</th>
								<th width="120px">
									专业
								</th>
								<th width="80px">
									班级
								</th>
								<th width="135px">
									宿舍
								</th>
							</tr>
						</thead>
						<tbody id="ttb">
							<tr id="pattern"  onclick='getStudentInfoDetail(this)'>
								<td >
									<span id="sid" style="display: none"></span>
								</td>
								<td >
									<span id="sno"></span>
								</td>
								<td >
									<span id="sname"></span>
								</td>
								<td >
									<span id="sgender"></span>
								</td>

								<td >
									<span id="scollege"></span>
								</td>
								<td >
									<span id="smajor"></span>
								</td>
								<td >
									<span id="sclass"></span>
								</td>
								<td >
									<span id="sdorm"></span>
								</td>

							</tr>
						</tbody>
					</table>
				</div>

				<table id="pageTable" align="center"
					style="visibility: hidden; margin-top: 30px; font: 12px;">
					<tr>
						<td id="pageTd"></td>
						<td>
							<select id="pageSelect" onchange="gotoPage(this);"></select>
						</td>
					</tr>
				</table>
				<div id="loading"
					style="display: none; margin-top: 20px; text-align: center; color: red; font: 13px;">
					Loading...
				</div> </span>


			<jsp:include page="/student/basicstudentinfo.jsp"></jsp:include>
	
	<script type='text/javascript'>
		 //todo解除basicsudentinfo.jsp里的enter键事件
	 //document.onkeydown -= subEv;
	 /*
	 if(navigator.userAgent.indexOf("MSIE")>0) { 
		 document.detachEvent(document.onkeydown);
		 document.onkeydown=subEvent;
	 }else{
	 	alert(1)
	 	document.removeEventListener("keyDown",subEv,false);
	 	document.addEventListener("keyDown",subEvent,false); 
		alert(2)
	 }
	 */

	 </script>
	</body>
</html>
